package cn.zbh.system.service.impl;

import cn.zbh.basic.service.impl.BasicServiceImpl;

import cn.zbh.system.domain.Menu;
import cn.zbh.system.domain.Role;
import cn.zbh.system.mapper.MenuMapper;
import cn.zbh.system.mapper.RoleMapper;
import cn.zbh.system.service.IMenuService;
import cn.zbh.system.service.IRoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;



@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl extends BasicServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Override
    @Transactional
    public void add(Role role) {
        roleMapper.add(role);
        //添加操作
        if(role.getMenus().size()>0){
            //t_role_menu
            roleMapper.addRole2Menu( role.getId(),role.getMenus());
        }
        if(role.getPermissions().size()>0){
            //t_role_permission
            roleMapper.addRole2Premission(role.getId(),role.getPermissions());
        }

    }

    @Override
    @Transactional
    public void delete(long rid) {
        // 删除 ， 先删除关联表，再删除自己

        roleMapper.deleteEmployeeRole(rid);
        roleMapper.deleteRoleMenu(rid);
        roleMapper.deleteRolePermission(rid);

        // 删除自己
        roleMapper.delete(rid);


    }

    @Override
    public void update(Role role) {
        // 修改
        roleMapper.update(role);

        // 先删除，再添加
        roleMapper.deleteRolePermission(role.getId());
        roleMapper.deleteRoleMenu(role.getId());

        //添加操作
        if(role.getMenus().size()>0){
            //t_role_menu
            roleMapper.addRole2Menu( role.getId(),role.getMenus());
        }
        if(role.getPermissions().size()>0){
            //t_role_permission
            roleMapper.addRole2Premission(role.getId(),role.getPermissions());
        }

    }
}
